import repository from '../repository'
// 购物车服务
class CartService {
  async addProduct (params) {
    const data = await repository.addProductIntoCart(params)
    if (data.code==500) {
      throw new Error('加入购物车失败:请先登录！' + data.msg)
    }
    return data
  }

  async getProductList (shopId) {
    const data = await repository.getCartProductList(shopId)
    if (data.code==500) {
      throw new Error('获取购物车商品列表失败:用户未登录' + data.msg)
    }
    return data.data
  }

  getCartTotal (productList) {
    if (!productList) {
      return 0
    }
    let total = 0
    productList.forEach(v => {
      total += v.amount
    })
    return total
  }

  async clearCart () {
    const data = await repository.clearCart()
    return data
  }

  async deleteCart(id){
    const data = await repository.deleteCart(id)
    return data
  }
}
export default new CartService()
